{$i+,r-,s-}
{$i modif.inc}
Program vector;
uses {$ifdef Windows}WinCrtx,WinDos,
     {$else}
       {$ifdef crtx}crtx,{$else}crt,{$endif}
     {$endif}
     {$ifdef fgraph}fGraph,{$else}
       {$ifdef graph}Graf6,graph,wind2,{$endif}   {For proper display graph or fgraph must be defined}
     {$endif}
     dos,Pictures;


{$ifdef fgraph}{$define graph}{$endif}
Label 1,10,20,30,40;

var a:string;
    obr:Picture; vbr:VectorPicture;
    i,j:integer;
    hromadne:Boolean;
    s:SearchRec;
    Options:PCVOpt;
    PassLProc:PercentLineProc;


Function PercentLineProc(l:integer;var p:AbstractPicture):Byte; far;
var i:longint;
begin
 {$ifdef  Windows}KeyPressed;{$endif}
 if l mod 8 <>0 then exit;
 if l<=0 then exit;
 i:=(longint(100)*l)div p.y;
 write(#13,i:7,'%');
{ if keypressed then halt(1);}
 PercentLineProc:=0;
end;

const Title:array[0..12]of char='Vectorizator'#0;
begin
{$ifdef Windows}
ScreenSize.X:=75;
ScreenSize.Y:=10;
WindMax:=ScreenSize;
Move(Title,WindowTitle,Sizeof(Title));
{$endif}

PassLProc.Init;
obr.Init;
vbr.Init;

{$ifdef graph}DirectVideo:=False;{$endif}
new(Options);
Options^.eps:=0.9;
Options^.tisk:=False;
Options^.AutoErase:=False;
nosound;
hromadne:=False;
writeln(#10#13'<<< Vector >>> Vectoriser of edge detected gray pictures (c)1994-95 F&TSoft');
if paramcount=0 then
	begin
1:	write('Enter name of input picture:');readln(a);
        if a='' then goto 1;
	assignpicture(obr,a);
	end;

for i:= 1 to paramcount do
	begin
        a:=ParamStr(i);
        if (a='/g')or(a='/G') then begin;Options^.Tisk:=True;end;
        if a='/?' then
        	begin
	        write(#13'VECTOR bitmap_file [vector_file] [/g] [number]'+
		      #10#13'    /g - show vectorization in progress'+
	      	      #10#13'    /? - this help'+
		      #10#13'    number - squrare quadratic error (range [0.01 - 5])');
		Goto 40;
                end;
        if a[1] in ['0'..'9','+'] then
        	begin
                val(a,Options^.eps,j);
                if(j<>0) then
                         begin
			 writeln('Error: Incorrect number ',a);
                         Goto 40;
                         end;
                continue;
                end;

        if (obr.filename=nil)or(obr.filename^='') then
          begin
          assignpicture(obr,a);
          for j:=1 to length(a) do if (a[j]='?')or(a[j]='*') then hromadne:=True;
          continue;
          end;
        if (vbr.filename=nil)or(vbr.filename^='') then
          begin
          assignpicture(vbr,a);
          continue;
          end;

        writeln(#13'Warning: unknown command ',a);
        end;

a:='';
if (obr.filename<>nil) then a:=obr.filename^;
if Hromadne then
	begin
        FindFirst(a,Anyfile,s);
        end;
10:
if Hromadne then assignPicture(obr,s.Name);
if (vbr.filename=nil)or(vbr.filename^='') then		{vec not assigned?}
	 begin
         a:=Obr.FileName^;
         for i:=1 to length(a)-1 do if a[i+1]='.' then break;
         a[0]:=chr(i);
         a:=a+'.VEC';
	 assignpicture(vbr,a);
         end;

write(#13' Loading ');ClrEol;
LoadPicture(obr,'',@PassLProc);
if (IOresult<>0)or(obr.data=nil) then
                begin
                writeln(#13'Cannot load input picture:''',obr.filename^,'''');
		GoTo 30; {fail load operation}
		end;

case obr.planes of 1:begin;Options^.pl:=1;Options^.Pa:=1;end;
		   8:begin;Options^.pl:=250;Options^.Pa:=10;end;
	     end;

{$ifdef graph}
if Options^.Tisk then
  begin
{$ifdef fgraph}
  InitGraph(0,1,'');
{$else}
  OpenWindow(1,1,MaxX,MaxY);
  OpenGraph;
{$endif}
  { Do graphics }
  SetColor(Green); {}
  view(obr);{}
  SetColor(White); {}
  end;
{$else}
Options^.Tisk:=False;
{$endif}


if not(Options^.Tisk) then Convert2Vector(obr,vbr,Options,@PassLProc)
		      else Convert2Vector(obr,vbr,Options,nil);
write(#13' saving ');ClrEol;
SavePicture(vbr,'',@PassLProc);

if not(Hromadne)and Options^.Tisk then
		begin
		sound(200);delay(500);nosound;
		Readln;
		end;
{$ifdef graph}
if Options^.Tisk then
  begin
  CloseGraph;
  {$ifndef fgraph}
  NastavXY;
  CloseWindow;
  {$endif}  
  end;
{$endif}
obr.Erase;
vbr.Erase;
20:
if Hromadne or not(Options^.Tisk) then
	begin
	writeln(#13'Image ''',obr.FileName^,''' processed.');
        end;
30:
if Hromadne then		{hledej dalsi obrazek}
	begin
        AssignPicture(vbr,'');
        findNext(s);
        if DosError=0 then Goto 10;
        end;
{writeln(MemAvail);}
40:
obr.Done;
vbr.Done;
end.
